package com.base.myproject.client.ui;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;

public class BaseAdvSearch {

	ContentPanel advsearch = null;
	 BaseStore bindbasestore = null;
	String advsearchstr = "";
	String targetcolumn = "";
	 TextField<String> search  = null;
	public BaseAdvSearch() {
		advsearch = new ContentPanel();
		init();
	}
/**
 * 绑定到数据
 * @param bs
 */

	public void bindBaseStore(BaseStore bs) {
		this.bindbasestore = bs;
	}

	public void init() {
		advsearch.setCollapsible(true);
		advsearch.setAnimCollapse(false);
		advsearch.setExpanded(false);
		advsearch.setHeading("高级查找");
		search = new TextField<String>();
		search.setName("search");
		search.setToolTip("高级查找，可能会花费你更长的时间.");
		search.setWidth("100%");
		Button button = new Button("查找");
		button.addSelectionListener(new SelectionListener(){

			@Override
			public void componentSelected(ComponentEvent ce) {
				// TODO Auto-generated method stub
				
			
				search() ;
			}

					
		});
		search.addKeyListener(new KeyListener(){
			
			public void componentKeyPress(ComponentEvent  ce)
			{
				
				if(ce.getKeyCode()==13) //回车
				{
					search() ;
				}
			}
		});
		advsearch.add(search);
		advsearch.add(button);
//		advsearch.setWidth("100%");
		advsearch.setAutoWidth(true);
	}

	public ContentPanel getAdvsearch() {
		return advsearch;
	}

	public void setAdvsearch(ContentPanel advsearch) {
		this.advsearch = advsearch;
	}

	public BaseStore getBindbasestore() {
		return bindbasestore;
	}

	public void setBindbasestore(BaseStore bindbasestore) {
		this.bindbasestore = bindbasestore;
	}

	public String getAdvsearchstr() {
		return advsearchstr;
	}

	public void setAdvsearchstr(String advsearchstr) {
		this.advsearchstr = advsearchstr;
	}

	public void search() {
		StoreInfo storeinfo = bindbasestore.getStoreinfo();
		
		targetcolumn = "";
		for(int i=0;i<	bindbasestore.getType().getFieldCount();i++)
		{
			targetcolumn +=bindbasestore.getType().getField(i).getName()+",";
		}
		targetcolumn = targetcolumn.substring(0, targetcolumn.length()-1);
		if(search.getValue()!=null)
		{
		setAdvsearchstr(search.getValue().toString());
		
		storeinfo.setAdvsearch(getAdvsearchstr()+"&targetcolumn="+targetcolumn);
		// bindbasestore.setStoreinfo(storeinfo);
		System.out.println(storeinfo.getAdvsearch());
		}
		else
		{
			storeinfo.setAdvsearch(null);
		}
		bindbasestore.reload();
	}
}
